const STORAGE_KEY = 'NAME_PY'
import axios from '../config'

export default {
  fetch() {
    var result = window.localStorage.getItem(STORAGE_KEY)
    if (!result) {
      axios.get('users')
        .then(function (res) {
          console.log('请求用户数据')
          if (res.data.code === 200) {
            window.localStorage.setItem(STORAGE_KEY, JSON.stringify(res.data.data))
          }
        }).catch(
        function (error) {
          console.log(error)
        }
      )
    }
    return JSON.parse(result)
  },
  get(py) {
    py = py.replace(/\s+/g, '')
    var all = JSON.parse(window.localStorage.getItem(STORAGE_KEY) || [])
    var reg = /^[A-Za-z]*$/
    var list = []
    for (let i in all) {
      if (reg.test(py)) {
        if (all[i].name_py.indexOf(py.toUpperCase()) !== -1) {
          // all[i].value = all[i].name
          list.push(all[i])
        }
      } else {
        if (all[i].name.indexOf(py) !== -1) {
          // all[i].value = all[i].name
          list.push(all[i])
        }
      }
    }
    return list
  }
}

